var express = require('express');
var router = express.Router();

let studentService = require("../src/services/studentServices")


router.get("/getstudentbypage", async function (req,res) {

  let {curPage,pageSize}= req.query
   console.log(curPage,pageSize)

  let result = await studentService.getStudentInfoByPage(curPage,pageSize);

 

  res.json({code:0 , ...result})
  
})

//新增学生
router.post("/addstudent",async function (req,res) {
  let{name,age,gender,tel}= req.body;
  let result = await studentService.addstudent({name:name,age:parseInt(age),gender:gender,tel:tel})
  res.json({code:0,msg:"添加成功"});
})

//删除学生
router.delete("/deletestudent/:stuId",async function (req,res) {
  let {stuId}= req.params;
  let result = await studentService.deleteStudent(stuId);

  if (result.deletedCount!=0) {
    res.json({code:0,msg:"删除成功"})
  }else{
    res.json({code:1,msg:"删除失败"})
  }


})

//修改学生
router.put("/updatastudent",async function (req,res) {
let {stuid,name,age,gender,tel}=req.body;
console.log(req.body);
let result = await studentService.updateStudentInfo(stuid,{name,age,gender,tel})

if (result) {
  res.json({code:0,msg:"修改成功"})
}else{
  res.json({code:1,msg:"修改失败"})
}
})

//查询学生详细
router.get("/showdeailinfo/:stuid",async function (req,res) {
  let {stuid}=req.params;
// return await studentService.showStudentDetailinfo(stuid)

let result = await studentService.showStudentDetailinfo(stuid)

if (result) {
  res.json({code:0,msg:"查询成功",result})
}else{
  res.json({code:1,msg:"查询失败",})
}
})



module.exports = router;
